package com.credit.jumu.playcard.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.credit.jumu.playcard.BuildConfig;
import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.adapter.GalleryAdapter;
import com.credit.jumu.playcard.base.BaseActivity;
import com.credit.jumu.playcard.bean.ProcessInfo;
import com.credit.jumu.playcard.cardapp.CardApp;
import com.credit.jumu.playcard.utils.DataSharedPreference;
import com.credit.jumu.playcard.utils.GlideUtil;
import com.credit.jumu.playcard.utils.JsonUtils;
import com.credit.jumu.playcard.utils.LogUtils;
import com.credit.jumu.playcard.utils.MD5Encoder;
import com.credit.jumu.playcard.utils.SignUtil;
import com.credit.jumu.playcard.utils.SystemUtil;
import com.credit.jumu.playcard.utils.ToastUtil;
import com.credit.jumu.playcard.utils.TypeUtils;
import com.credit.jumu.playcard.utils.UrlsUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.makeramen.roundedimageview.RoundedImageView;
import com.yuyh.library.imgsel.widget.DividerGridItemDecoration;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2017/10/31/031.
 */

public class ProductDetailActivity extends BaseActivity {
	TextView loan_range, loan_time, day_rate, detail_tv, applay_person_num, fast_loan, loan_range2,
			loan_time2, tv_required_data, tv_audit_desc, product_detail_now_loan;
	RecyclerView mRecyclerView;
	RoundedImageView detail_img;
	String detailData;
	ArrayList seqList = new ArrayList<ProcessInfo>();
	GalleryAdapter galleryAdapter;
	private DataSharedPreference ds;
	String proId = "";
	ImageView bt_back_detail;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (null == ds) {
			ds = new DataSharedPreference(this);
		}
		setContentView(R.layout.fragment_product_detail);
		Intent intent = getIntent();
		detailData = intent.getStringExtra("responseData");
		initView();

	}

	private void initView() {
		loan_range = (TextView) findViewById(R.id.loan_range);
		loan_time = (TextView) findViewById(R.id.loan_time);
		day_rate = (TextView) findViewById(R.id.day_rate);
		detail_tv = (TextView) findViewById(R.id.detail_tv);
		applay_person_num = (TextView) findViewById(R.id.applay_person_num);
		fast_loan = (TextView) findViewById(R.id.fast_loan);
		loan_range2 = (TextView) findViewById(R.id.loan_range2);
		loan_time2 = (TextView) findViewById(R.id.loan_time2);
		tv_required_data = (TextView) findViewById(R.id.tv_required_data);
		tv_audit_desc = (TextView) findViewById(R.id.tv_audit_desc);
		detail_img = (RoundedImageView) findViewById(R.id.detail_img);
		mRecyclerView = (RecyclerView) findViewById(R.id.horizontallistview1);
		bt_back_detail = (ImageView) findViewById(R.id.bt_back_detail);
		//设置布局管理器
		LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
		linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		mRecyclerView.setLayoutManager(linearLayoutManager);
		galleryAdapter = new GalleryAdapter(getApplication(), seqList);
		mRecyclerView.setAdapter(galleryAdapter);
		product_detail_now_loan = (TextView) findViewById(R.id.product_detail_now_loan);
		product_detail_now_loan.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				initProducturlGo(proId);
			}
		});
		bt_back_detail.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ProductDetailActivity.this.finish();
			}
		});

		setData();

	}

	private void setData() {
		if (TextUtils.isEmpty(detailData)) return;
		JSONObject responseJson = JsonUtils.getJsonObject(detailData);
		JSONObject productDetail = TypeUtils.getJsonObject(responseJson, "productDetail");

		proId = TypeUtils.getJsonString(productDetail, "proId", "");
		loan_range.setText(TypeUtils.getJsonString(productDetail, "loanMoneyA", "") + "-" + TypeUtils.getJsonString(productDetail, "loanMoneyB", "") + "元");
		loan_time.setText(TypeUtils.getJsonString(productDetail, "loanTermA", "") + "-" + TypeUtils.getJsonString(productDetail, "loanTermB", ""));
		day_rate.setText(TypeUtils.getJsonString(productDetail, "rate", ""));
		GlideUtil.loadImage(this, TypeUtils.getJsonString(productDetail, "prodLogo", ""), detail_img);
		detail_tv.setText(TypeUtils.getJsonString(productDetail, "prodName", ""));
		applay_person_num.setText(TypeUtils.getJsonString(productDetail, "applyNum", "") + "人");
		fast_loan.setText(TypeUtils.getJsonString(productDetail, "approvalTime", ""));
		loan_range2.setText(TypeUtils.getJsonString(productDetail, "loanMoneyA", "") + "-" + TypeUtils.getJsonString(productDetail, "loanMoneyB", "") + "元");
		loan_time2.setText(TypeUtils.getJsonString(productDetail, "loanTermA", "") + "-" + TypeUtils.getJsonString(productDetail, "loanTermB", ""));
		tv_required_data.setText(TypeUtils.getJsonString(productDetail, "requiredData", ""));
		tv_audit_desc.setText(TypeUtils.getJsonString(productDetail, "auditDesc", ""));
		JSONArray seqDatas = TypeUtils.getJsonArray(productDetail, "seq");
		if (null != seqDatas && seqDatas.size() > 0) {
			for (int i = 0; i < TypeUtils.getJsonArraySize(seqDatas); ++i) {
				ProcessInfo processInfo = new ProcessInfo();
				JSONObject process = seqDatas.getJSONObject(i);
				processInfo.setIcon(TypeUtils.getJsonString(process, "img", ""));
				processInfo.setText(TypeUtils.getJsonString(process, "title", ""));
				seqList.add(processInfo);
			}
			galleryAdapter.notifyDataSetChanged();
		}

	}

	private void initProducturlGo(String id) {

		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		if (TextUtils.isEmpty(ds.getAccountId()) || TextUtils.isEmpty(ds.getToken()))
			ToastUtil.showShortToast("请先登录");
		mapSign.put("accountId", ds.getAccountId());
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("proId", id);
		mapSign.put("source", CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("token", ds.getToken());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", ds.getAccountId());
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CardApp.intefaceType);
		mapPramas.put("proId", id);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", ds.getToken());
		mapPramas.put("version", BuildConfig.VERSION_NAME);
		showLoading();
		OkGo.<String>post(UrlsUtils.HOME_PRODUCT_URL)
				.tag(this)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								dismissLoading();
								LogUtils.println("zhangyiyionSuccessURL_PRODUCT_TOGO------" + response.body());
								if (!TextUtils.isEmpty(response.body())) {
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									String code = TypeUtils.getJsonString(responseJson, "code", "");
									String message = TypeUtils.getJsonString(responseJson, "message", "");
									String goUrl = TypeUtils.getJsonString(responseJson, "goUrl", "");

									if (!"9999".equals(code)) {
										ToastUtil.showShortToast(message);
									} else {
										Intent intent = new Intent(ProductDetailActivity.this, MyWebActivity.class);
										intent.putExtra("url", goUrl);
										startActivity(intent);
									}
								}
							}

							@Override
							public void onError(Response<String> response) {
								dismissLoading();
								LogUtils.println("zhangyiyionErrorURL_PRODUCT_TOGO------" + response.body());
							}
						}
				);
	}
}
